package org.colafries.admin.entity.domain;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Base64;

/**
 * @author colafries
 * @version v1.0
 * @ClassName DataSource
 * @Description
 * @date Created in 2024/3/18
 **/
@Data
@TableName("t_database")
@NoArgsConstructor
@Accessors(chain = true)
public class DataBase {
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    @TableField("db_type_code")
    private Integer dbTypeCode;
    @TableField("db_name")
    private String dbName;
    @TableField("db_host")
    private String dbHost;
    @TableField("db_port")
    private String dbPort;
    @TableField("db_url_args")
    private String dbUrlArgs;

    @TableField("db_user")
    private String dbUser;
    @TableField("db_pwd")
    private String dbPwd;
    @TableField("db_desc")
    private String dbDesc;

    @TableField(value = "create_time",fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
    @TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @TableField("update_user")
    private String updateUser;

    /**
     * 获取数据源key
     * @return
     */
    public String getKey() {
        String key = dbHost + dbPort + dbUser + dbPwd + dbUrlArgs;
        return Base64.getEncoder().withoutPadding().encodeToString(key.getBytes(StandardCharsets.UTF_8));
    }

    public String getUrl(){
        return "jdbc:{SubProtocol}://" + this.dbHost + ":" + this.dbPort + "/?" + this.dbUrlArgs;
    }
}
